package request

import (
	"Squid/models"
	"gorm.io/gorm"
	"reflect"
)

// Job 分页参数
type Job struct {
	Paging
	Name      string `json:"name"`
}

// ValidateJobSortFile 检验字段是否在结构体中
func ValidateJobSortFile(filed string) bool {
	var (
		job *models.Job
		ele reflect.Type
	)

	job = &models.Job{}
	ele = reflect.TypeOf(job).Elem()
	for i := 0; i < ele.NumField(); i++ {
		if ele.Field(i).Tag.Get("json") == filed {
			return true
		}
	}
	return false
}

func JobSelectOrWhere(name string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if name == "" {
			return db
		} else {
			return db.Where("name LIKE ?", "%"+name+"%")
		}
	}
}
